var callerManager = (function ($) {

	function Module(context) {
		var me = this;
		me.context = context;
		me.$storage = context.storage;
		me.ui = context.ui;
		me.topCallerJoinId = null;// 当前对话的访客ID
		me.callerList = {};
	}

	/**
	 * 新增加一个访客
	 * @param userInfo
	 */
	Module.prototype.addCaller = function (userInfo) {
		var me = this,
			callerJoinId = userInfo.join_id;
		me.callerList[callerJoinId] = userInfo;
		me.setStorageCallerList();

		var obj = {};
		obj.callerJoinId = callerJoinId;
		obj.status = userInfo.status || 1;
		me.updateCallerStatus(obj);
	};

	Module.prototype.deleteCaller = function (callerJoinId) {
		var me = this;
		if (me.callerList[callerJoinId]) {
			delete me.callerList[callerJoinId];
			me.setStorageCallerList();
		}
	};

	/**
	 * 设置访客属性
	 * @param callerJoinId
	 * @param attr
	 * @param val
	 */
	Module.prototype.setCallerAttr = function (callerJoinId, attr, val) {
		var callerInfo = this.getCallerInfo(callerJoinId);
		if (callerInfo) {
			callerInfo[attr] = val;
			this.setStorageCallerList();
		}
	};

	/**
	 * 更新存储里的访客列表
	 */
	Module.prototype.getStorageCallerList = function () {
		return this.$storage.get('callerList', '');
	};

	/**
	 * 更新存储里的访客列表
	 */
	Module.prototype.setStorageCallerList = function () {
		var me = this;
		me.$storage.set('callerList', me.callerList);
	};

	Module.prototype.setTopCaller = function (callerJoinId) {
		this.topCallerJoinId = callerJoinId;
		this.$storage.set('topCallerJoinId', callerJoinId);
	};

	Module.prototype.getTopCaller = function () {
		return this.$storage.get('topCallerJoinId');
	};

	/**
	 * 设置访客的状态
	 * @param flag 0为状态未知， 1为在线，2为下线，3为已转接到其它客服
	 */
	Module.prototype.setCallerStatus = function (callerJoinId, flag) {
		var me = this;
		if (me.callerList[callerJoinId]) {
			me.setCallerAttr(callerJoinId, 'status', flag);
			var liId = 'c' + callerJoinId,
				$li = $('#' + liId);
			if (flag == 2) {
				$li.find('.callerLabel').attr('class', 'callerLabel offline');
			} else if (flag == 3) {
				$li.find('.callerLabel').attr('class', 'callerLabel handover');
			}
		}
	};

	/**
	 * 获取访客状态
	 * @param callerJoinId
	 * @returns {string}
	 */
	Module.prototype.getCallerStatus = function (callerJoinId) {
		var me = this,
			flag = '';
		if (me.callerList[callerJoinId]) {
			flag = me.callerList[callerJoinId].status;
		}
		return flag;
	};

	/**
	 * // 获取访客状态
	 * @param callerJoinId
	 * @returns {*}
	 */
	Module.prototype.getCallerInfo = function (callerJoinId) {
		var me = this,
			info = null;
		if (me.callerList[callerJoinId]) {
			info = me.callerList[callerJoinId];
		}
		return info;
	};

	/**
	 * 获取访客属性
	 * @param callerJoinId
	 * @param attr
	 * @returns {*}
	 */
	Module.prototype.getCallerAttr = function (callerJoinId, attr) {
		var callerInfo = this.getCallerInfo(callerJoinId),
			val = null;
		if (callerInfo) {
			val = callerInfo[attr];
		}
		return val;
	};

	/**
	 * 批量恢复访客，如果访客之前的状态为在线，则需要重新确认访客是否仍在线
	 */
	Module.prototype.batchRestoreCaller = function (listData) {
		if ($.isPlainObject(listData)) {
			var me = this;
			$.each(listData, function (k, caller) {
				caller.lastStatus = caller.status;
				if (caller.status == 1) {
					caller.status = 2;
				}
				me.context.setNewCaller(caller);
				if (caller.status == 2) {
					me.context.getCallerStatus(caller.join_id);
				}
			});
		}
	};

	/**
	 * 从存储里恢复访客列表
	 */
	Module.prototype.restore = function () {
		var me = this,
			listData = me.getStorageCallerList();
		me.batchRestoreCaller(listData);
		me.topCallerJoinId = me.getTopCaller();
	};

	/**
	 * 获取访客列表的元素
	 */
	Module.prototype.getCallerLi$Element = function (callerJoinId) {
		var liId = 'c' + callerJoinId;
		return $('#' + liId);
	};

	/**
	 * 更新访客状态
	 */
	Module.prototype.updateCallerStatus = function (obj) {
		var $li = this.getCallerLi$Element(obj.callerJoinId),
			icoCls = 'offline';
		if (obj.status == 1) {
			icoCls = 'online'
		} else if (obj.status == 3) {
			icoCls = 'handover';
		}
		this.setCallerStatus(obj.callerJoinId, obj.status);
		$li.find('.callerLabel').attr('class', 'callerLabel ' + icoCls);
	};

	Module.prototype.setCallerInfo = function (userInfo) {
		var me = this;
		var html = me.ui.buildCallerInfoBox(userInfo);
		var dlg = me.ui.getDialog(userInfo.join_id),
			$box = dlg.$box;
		$box.find('.js-callerInfo').html(html);
	};

	return Module;
})(jQuery);